/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jaiz.twitter.dao;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jaiz.twitter.model.DBManager;
import org.jaiz.twitter.model.Tweet;
import org.jaiz.twitter.util.Query;

/**
 *
 * @author JesusMata
 */
public class TweetDAO extends DBManager implements IDAO<Tweet, Integer> {
    
    @Override
    public void create(Tweet e) {
        try {
            PreparedStatement statement = createStatement(Query.getQuery("tweet.create"));
            statement.setInt(1, e.getId());
            statement.setString(2, e.getUsuario().getUserName());
            statement.setString(3, e.getTexto());
            statement.setDate(4, e.getFecha());
            statement.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(TweetDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
                
    }

    @Override
    public void delete(Tweet e) {
        try {
            PreparedStatement statement = createStatement(Query.getQuery("tweet.delete"));
            statement.setInt(1, e.getId());
            statement.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(TweetDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Tweet e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Collection<Tweet> getAll() {
        try {
            Collection<Tweet> tweets = new ArrayList<Tweet>();
            UsuarioDAO udao  = new UsuarioDAO(); //Para obtener el usuario
            
            PreparedStatement statement = createStatement(Query.getQuery("tweet.getAll"));
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                Tweet tweet = new Tweet();
                tweet.setId(rs.getInt("idTweet"));
                tweet.setUsuario(udao.getById(rs.getString("usuario")));
                tweet.setTexto(rs.getString("texto"));
                tweet.setFecha(rs.getDate("fecha"));
                tweets.add(tweet);
            }
            return tweets;
        } catch (SQLException ex) {
            Logger.getLogger(TweetDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    @Override
    public Tweet getById(Integer i) {
        try {
            UsuarioDAO udao  = new UsuarioDAO(); //Para obtener el usuario
            
            PreparedStatement statement = createStatement(Query.getQuery("tweet.getById"));
            ResultSet rs = statement.executeQuery();
            if(rs.next()){
                Tweet tweet = new Tweet();
                tweet.setId(rs.getInt("idTweet"));
                tweet.setUsuario(udao.getById(rs.getString("usuario")));
                tweet.setTexto(rs.getString("texto"));
                tweet.setFecha(rs.getDate("fecha"));
                return tweet;
            }
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(TweetDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
}
